package com.imufe.chemicalenterprisedangerwarning.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imufe.chemicalenterprisedangerwarning.entity.query.ChemicalQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.query.CreateGroup;
import com.imufe.chemicalenterprisedangerwarning.entity.query.UpdateGroup;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.ChemicalDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.ChemicalVO;
import com.imufe.chemicalenterprisedangerwarning.service.ChemicalService;
import com.imufe.chemicalenterprisedangerwarning.common.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 化学品 前端控制器
 * </p>
 *
 * @author BaiBan
 * @since 2022-09-21
 */
@Api(tags = "化学品")
@RestController
@RequestMapping("/chemical")
public class ChemicalController {

    @Autowired
    ChemicalService service;

    @ApiOperation(value = "化学品列表", notes = "化学品的列表(分页)")
    @GetMapping("/page")
    public ResponseEntity<IPage<ChemicalDetailedVO>> page(@RequestParam(value = "current", defaultValue = "1") Long current) {
        IPage<ChemicalDetailedVO> page = service.page(current);
        return ResponseEntity.success(page);
    }

    @ApiOperation(value = "化学品选项表", notes = "创建或修改企业化学品时下拉选项表")
    @GetMapping("/option/list")
    public ResponseEntity<List<ChemicalVO>> list() {
        return ResponseEntity.success(service.optionList());
    }

    @ApiOperation(value = "新增", notes = "新增一个化学品，返回新增的化学品")
    @PostMapping("/create")
    public ResponseEntity<ChemicalDetailedVO> create(@RequestBody @Validated(CreateGroup.class) ChemicalQuery query) {
        ChemicalDetailedVO detailedVO = service.create(query);
        return ResponseEntity.success(detailedVO);
    }

    @ApiOperation(value = "删除", notes = "删除一个化学品")
    @DeleteMapping("/delete")
    public ResponseEntity<?> delete(@RequestBody Long id) {
        service.removeById(id);
        return ResponseEntity.success();
    }

    @ApiOperation(value = "修改", notes = "修改一个化学品，返回修改后的化学品信息")
    @PutMapping("/update")
    public ResponseEntity<ChemicalDetailedVO> update(@RequestBody @Validated(UpdateGroup.class) ChemicalQuery query) {
        ChemicalDetailedVO detailedVO = service.update(query);
        return ResponseEntity.success(detailedVO);
    }
}